package com.heima.schedule.feign;


import com.heima.api.schedule.ScheduleClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.schedule.dtos.Task;
import com.heima.schedule.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class scheduleClientController implements ScheduleClient {


    @Autowired
    private TaskService taskService;

    /**
     * 添加延迟任务
     *
     * @param task
     * @return
     */
    @PostMapping("/api/v1/schedule/addTask")
    @Override
    public ResponseResult addTask(@RequestBody Task task) {

        long taskId = taskService.addTask(task);
        return ResponseResult.okResult(taskId);
    }

    /**
     * 取消任务
     *
     * @param taskId
     * @return
     */
    @GetMapping("/api/v1/schedule/cancel/{taskId}")
    @Override
    public ResponseResult cancelTask(@PathVariable Long taskId) {

        boolean result = taskService.cancelTask(taskId);
        return ResponseResult.okResult(result);
    }

    /**
     * 按照类型和优先级拉取任务
     *
     * @param type
     * @param priority
     * @return
     */
    @GetMapping("/api/v1/task/{type}/{priority}")
    public ResponseResult poll(@PathVariable("type") int type, @PathVariable("priority") int priority) {
        return ResponseResult.okResult(taskService.poll(type,priority));
    }
}
